package ac.neec.mio.ui.adapter;

import java.util.List;

import ac.neec.data.MajorHeading;
import ac.neec.data.SpecificActivities;
import ac.neec.db.service.SQLManager;
import ac.neec.mio.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TypeSelectorAdapter extends BaseExpandableListAdapter{
    private static int METS1_COLOR;
    private static int METS2_COLOR;
    private static int METS3_COLOR;
    private static int METS4_COLOR;
    private static float METS1_RANGE;
    private static float METS2_RANGE;
    private static float METS3_RANGE;
    private static float METS4_RANGE;

    private Context mContext;
    private LayoutInflater layoutInflater;
    private List<MajorHeading> groupData;
    private List<List<SpecificActivities>> childData;

    public TypeSelectorAdapter( Context context ) {
        mContext = context;
        layoutInflater = (LayoutInflater) context.getApplicationContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE );

        codeDataInit();
        adapterDataInit();
    }

    private void codeDataInit() {
        METS1_COLOR = mContext.getResources().getColor( R.color.mets1_color );
        METS2_COLOR = mContext.getResources().getColor( R.color.mets2_color );
        METS3_COLOR = mContext.getResources().getColor( R.color.mets3_color );
        METS4_COLOR = mContext.getResources().getColor( R.color.mets4_color );

        METS1_RANGE = Float.valueOf( mContext.getResources().getString( R.string.mets1 ) );
        METS2_RANGE = Float.valueOf( mContext.getResources().getString( R.string.mets2 ) );
        METS3_RANGE = Float.valueOf( mContext.getResources().getString( R.string.mets3 ) );
        METS4_RANGE = Float.valueOf( mContext.getResources().getString( R.string.mets4 ) );
    }

    private void adapterDataInit() {
        groupData = SQLManager.selectMajorHeadingAll();
        childData = SQLManager.selectSpecificActivitiesCategories();
    }

    @Override
    public Object getChild( int groupPosition, int childPosition ) {
        return childData.get( groupPosition ).get( childPosition );
    }

    @Override
    public long getChildId( int groupPosition, int childPosition ) {
        return childPosition;
    }

    @Override
    public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView,
                              ViewGroup parent ) {
        View view = LayoutInflater.from( mContext ).inflate( R.layout.adapter_selector_type_child, null );
        TextView mets = (TextView) view.findViewById( R.id.adapter_type_chi_mets );
        TextView detail = (TextView) view.findViewById( R.id.adapter_type_chi_detail );
        TextView detailRow = (TextView) view.findViewById( R.id.adapter_type_chi_detail_row );
        RelativeLayout layout = (RelativeLayout) view.findViewById( R.id.adapter_type_chi_layout );

        SpecificActivities item = childData.get( groupPosition ).get( childPosition );
        mets.setText( item.getMetsString() );
        detail.setText( item.getMajorHeadingTitleJP() );
        detailRow.setText( item.getDetailJP() );

        int code = calcColorCode( item.getMets() );
        item.setColorCode( code );

        layout.setBackgroundColor( code );

        return view;
    }

    private int calcColorCode( float mets ) {
        int code = 0;
        if ( METS4_RANGE < mets ) {
            code = METS4_COLOR;
        } else if ( METS3_RANGE < mets ) {
            code = METS3_COLOR;
        } else if ( METS2_RANGE < mets ) {
            code = METS2_COLOR;
        } else if ( METS1_RANGE < mets ) {
            code = METS1_COLOR;
        }
        return code;
    }

    @Override
    public int getChildrenCount( int groupPosition ) {
        return childData.get( groupPosition ).size();
    }

    @Override
    public Object getGroup( int groupPosition ) {
        return groupData.get( groupPosition );
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public long getGroupId( int groupPosition ) {
        return groupPosition;
    }

    @Override
    public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent ) {
        View view = LayoutInflater.from( mContext ).inflate( R.layout.adapter_select_type_parent, null );
        TextView titleJP = (TextView) view.findViewById( R.id.adapter_type_pare_jp );
        TextView titleENG = (TextView) view.findViewById( R.id.adapter_type_pare_eng );

        MajorHeading item = groupData.get( groupPosition );
        titleJP.setText( item.getTitleJP() );
        titleENG.setText( item.getTitleENG() );
        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable( int groupPosition, int childPosition ) {
        return true;
    }

}
